package com.zito.health.controller;

import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.enums.ResultCode;
import com.zito.health.common.result.PlatformResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.core.shiro.ZitoToken;
import com.zito.health.entity.User;
import com.zito.health.service.UserService;
import com.zito.health.util.CacheUtil;
import com.zito.health.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: gxt
 * @time: 2019/10/12 10:52
 */
@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping
    public PlatformResult login(UserVo user){
        if(user.getUsername()==null||user.getPassword()==null||
                "".equals(user.getUsername())||"".equals(user.getPassword())){
//            verifyResult.setResult(false);
//            verifyResult.setMsg("用户名或密码为空");
            return PlatformResult.failure("用户名或密码不能为空");
        }

//        VerifyResult verifyResult = new VerifyResult();
        PlatformResult platformResult = new PlatformResult();

        User u =new User();
        u.setUsername(user.getUsername());
        u.setEnabled(1);//可用
        u = userService.queryOne(u);
        if(u!=null){
            int flag=0;
            if(user.getType()==1){//实验室账号
                Integer[] sys={1,2,3,6,7,9};
                for(int i=0;i<sys.length;i++){
                        if(sys[i] == u.getRoleId()){
                            flag = 1;
                        }
                    }
            }else{//科研账号
                Integer[] ky={1,2,4,5,8,10};
                for(int i=0;i<ky.length;i++){
                    if(ky[i] == u.getRoleId()){
                        flag = 1;
                    }
                }
            }
            if(flag==0){//说明登录的功能不对
                return PlatformResult.failure("没有权限");
            }
        }else{
            return PlatformResult.failure("账号不存在或密码错误");
        }
        Subject subject = SecurityUtils.getSubject();
        ZitoToken token = new ZitoToken(user.getUsername(), user.getPassword(),false,"",1);
        String error = null;
        try {
            subject.login(token);
            CacheUtil.initCacheUser(user.getUsername());
            platformResult.setStatus(ResultCode.SUCCESS.code());
            user.setPassword(null);
            User usr=userService.queryOne(user);
            usr.setPassword(null);
            platformResult.setData(usr);
        }  catch (IncorrectCredentialsException e) {
            /**用户名/密码错误*/
            error = ResultCode.USER_LOGIN_ERROR.message();
            platformResult.setStatus(ResultCode.USER_LOGIN_ERROR.code());
            platformResult.setMsg(error);
        }  catch (AuthenticationException e) {
            error = ResultCode.USER_LOGIN_ERROR.message();
            platformResult.setStatus(ResultCode.USER_LOGIN_ERROR.code());
            platformResult.setMsg(error);
        }
        return platformResult;
    }
}
